/*
Contains:
	an overrided Move() proc
	an overrided Entered() proc
	variables related to spell creation
	procs related to spell creation
*/

player

	icon = 'monk.dmi'
	icon_state = "temp"

	Entered(atom/movable/Obj,atom/OldLoc)
		..()
		update_grids()

	Exited(atom/movable/obj)
		..()
		update_grids()

	Login()
		sight |= BLIND
		..()

	Logout()
		del src

	Write(savefile/F)
		F["version"] << SAVEFILE_VERSION
		..()
		F["save_x"] << x
		F["save_y"] << y
		F["save_z"] << z

	Read(savefile/F)
		loc = locate(F["save_x"],F["save_y"],F["save_z"])
		var/version
		F["version"] >> version
		if(isnull(version)) version = 0

		switch(version)
			if(1,2)
				if(isnull(hp))
					hp = new /stat(0,100,100)
					hp.associated_bar = "hp"
				if(isnull(exp))
					exp = new /stat(0,0,50)
					exp.associated_bar = "exp"
				//TODO: set up hp based on class/level as well
		/*
		TODO:
			when modifying levelup, check base stats here
		*/
		..()
		if(hp)	{hp.attached = src; hp.update()}
		if(exp)	{exp.attached = src; exp.update()}

		sight &= ~BLIND
		update_grids()

	Move()

		if(!can_move) return
		can_move = 0
		spawn(move_delay)
			can_move=1

		. = ..()

		if(!can_cast)
			spawn for(var/cast_bar/cb)
				if(cb.target == src)
					cb.end()

		for(var/monster/m in range(13,src))
			if(m.sleeping)
				m.sleeping = 0
				spawn() m.ai()

	var
		slot

		stat
			exp

		list
			learned			= list()
			orbs_found		= list()

			known_prefixes	= list()
			known_roots		= list()
			known_suffixes 	= list()

			verified_names	= list()

			ink_vials		= list()
			orbs			= list()

			tmp
				hotkeys
				buttons

	alliance = PLAYER

	parent_type = /mob

	proc
		determine_exp_gain(o_lvl)
			spawn()
				var/dif = o_lvl - level
				switch(dif)
					if(-6 to -MAX_LEVEL) gain_exp(0)
					if(-5 to -4) 		 gain_exp(1)
					if(-3 to -1) 		 gain_exp(2)
					if(0)				 gain_exp(3)
					if(1 to 2) 			 gain_exp(4)
					if(3 to 4) 		 	 gain_exp(6)
					if(5 to 6) 			 gain_exp(8)
					else	   			 gain_exp(10)

		bit2class(bit)
			if(bit & MAGICAL) return "magical"
			if(bit & SUPPORT) return "support"
			if(bit & RESTORE) return "restore"
			if(bit & RANGED ) return "range"
			if(bit & DARKMAG) return "dark-"
			if(bit & LIGHTMAG)return "light-"

		save_stats_txt(slot)
			if(fexists(client.path+"char[slot]/character.txt")) fdel(client.path+"char[slot]/character.txt")
			text2file("[name]\n[bit2class(class)]\n[level]\n[hp.get_current()]/[hp.get_maximum()]", client.path+"char[slot]/character.txt")

		save(slot)
			if(!client) return
			save_stats_txt(slot)
			if(fexists(client.path+"char[slot]/character.sav")) fdel(client.path+"char[slot]/character.sav")
			var/savefile/f = new (client.path+"char[slot]/character.sav")
			f << src
			fdel(client.path+"char[slot]/char_bak.txt")
			f.ExportText("/",file(client.path+"char[slot]/char_bak.txt"))

		set_up_character(slot)
			var/list/l = dd_file2list(client.path+"char[slot]/character.txt")
			if(l.len < 2) return
			name = l[1]
			src.slot = slot
			switch(l[2])
				if("magical") class |= MAGICAL
				if("support") class |= SUPPORT
				if("restore") class |= RESTORE
				if("ranged" ) class |= RANGED
			//TODO: assign icons

			hp = new /stat(0,100,100)
			hp.attached = src
			hp.associated_bar = "hp"
			hp.update()

		set_up_grid(this_grid, other_text)
			src << output(null, "spell_create.[this_grid]")

			var/list/known = vars["known_[lowertext(this_grid)]"]
			for(var/x in known)
				src << output("<a href=\"?add_[other_text]=[x]\">[x]</a> [known[x] ? " - [known[x]]" : ""]","spell_create.[this_grid]")

		set_up_spell_grids()
			set_up_grid("Prefixes", "prefix")
			set_up_grid("Roots", "root")
			set_up_grid("Suffixes", "suffix")

		draw_hotkeys()

			clear_hotkeys()

			var/book/b = BOOK
			for(var/x = 1 to b.capacity)
				var/hotkey/h = new /hotkey(b)
				if(b.spells.len < x) b.spells += null
				h.screen_loc = "[x],1"
				client.screen += h
				hotkeys += h

			update_hotkeys()

		clear_hotkeys()
			for(var/o in hotkeys) del o
			hotkeys = new/list()
			client.screen = new/list()

		update_hotkeys()
			var/book/b = BOOK
			for(var/x = 1 to b.capacity)
				if(b.spells.len >= x && b.spells[x])
					var/hotkey/h = hotkeys[x]
					h.set_spell(b.spells[x])

		update_grids()
			var/x=1
			for(var/book/b in contents)
				src << output(b, "default.books:1,[x++]")
			src << output(null, "default.books:1,[x++]")
			x=1
			for(var/armor/a in contents)
				src << output(a, "default.armor:1,[x++]")
			src << output(null, "default.armor:1,[x++]")

	death(mob/killer)
		loc = locate(1,1,1)
		hp.set_current(hp.get_maximum()/2)

	gain_exp(amount)

		if(!exp)
			exp = new /stat(0,0,50)
			exp.associated_bar = "exp"
			exp.attached = src

		exp.increase(amount)

		if(exp.at_max())
			levelup()

	levelup()

		//you can only be the level that your orb amount dictates
		if(level < orbs.len)

			//add level
			..()
			level++

			//reset exp
			exp.set_max(exp.get_maximum() + level*50)
			exp.set_current(0)

			//more hp
			hp.set_max(hp.get_maximum() + (round(hp.get_maximum()*((level/50)))))
			hp.set_current(hp.get_maximum())

	target(mob/target)
		if(ispriest(target)) return

		client.images = new/list()

		if(src.target == target)
			src.target = null
			return

		src.target = target
		var/image/i = image('stuff.dmi',target,"target")
		i.pixel_y = 32
		src << i
